//
//  OwnerViewController.m
//  FeiYang
//
//  Created by User on 15/10/12.
//  Copyright © 2015年 feiyang. All rights reserved.
//

#import "OwnerViewController.h"
#import "LoginViewController.h"
#import "ProblemFeedBackViewController.h"
#import "ChangePswViewController.h"
#import "MyInfoViewController.h"
#import "ProblemFeedBackViewController.h"
#import "OfflineViewController.h"
#import "AboutUsViewController.h"
@interface OwnerViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
{
//    NSIndexPath *index;
}
@property (nonatomic,retain)UITableView *myTableView;
@property (nonatomic,retain)UIButton *headBtn;//头像的按钮
@property (nonatomic,retain)UIView *bgView;
@property (nonatomic,retain)UIButton *loginBtn;
@property (nonatomic,retain)UIButton * exitBtn;
@property (nonatomic,retain)NSArray *firstGroupImgNameArr;
@property (nonatomic,retain)NSArray *firstTitleNameArr;
@property (strong, nonatomic)UIView *backColorView;
//@property (strong, nonatomic)UIView *logoView;
@property (strong, nonatomic) UILabel *loginLabel;


@end

@implementation OwnerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    TopView *topView = [[TopView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:@"个人中心"];
    [self.view addSubview:topView];
    
    self.view.backgroundColor = [UIColor hexStringToColor:@"#f3f3f3"];
    [self layoutTableview];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    if ([AccountTools isLogin]==NO) {
        
        [self.loginBtn setTitle:@"注册/登录" forState:UIControlStateNormal];
        self.loginBtn.frame = CGRectMake(NW(self.headBtn)+16.0f,42.0f, 120.0f, 30.0f);
        self.loginLabel.hidden = NO;
        [self.loginBtn setTitleColor:kColorTEXT forState:UIControlStateNormal];
        self.loginBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
        self.loginBtn.enabled = YES;
        self.exitBtn.hidden = YES;
        self.headBtn.enabled = NO;
        
        
    }else{
       
        NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",Index_Url_Test,[[NSUserDefaults standardUserDefaults]objectForKey:@"userHeadPhoto"]]];
        [self.headBtn sd_setImageWithURL:URL forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"img_default_head"]];
        [self.loginBtn setTitle:[[NSUserDefaults standardUserDefaults]objectForKey:@"userName"] forState:UIControlStateNormal];
        [self.loginBtn setTitleColor:kColorTEXT forState:UIControlStateNormal];
        self.loginBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
         self.loginBtn.frame = CGRectMake(NW(self.headBtn)+16.0f,60.0f, 120.0f, 30.0f);
        self.loginBtn.layer.borderColor = [[UIColor clearColor]CGColor];
        self.exitBtn.hidden = NO;
        self.loginLabel.hidden = YES;
        self.exitBtn.alpha = 1;
        self.loginBtn.enabled = NO;
        self.headBtn.enabled = YES;
        
    }
    
    [self.myTableView reloadData];

    
}
- (void)layoutTableview
{
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(16.0f ,64.0f,SCREEN_WIDTH-32.0f ,SCREEN_HEIGHT-64.0f-49.0f) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor clearColor];
    self.myTableView.showsVerticalScrollIndicator = NO;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.bounces = TRUE;
    [self.view addSubview:self.myTableView];
    
    
    
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH , 130.0f) ];

    
    
    self.headBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.headBtn setImage:[UIImage imageNamed:@"img_default_head"] forState:UIControlStateNormal];
    self.headBtn.frame = CGRectMake(44.0f, 42.0f, 58.0f, 58.0f);
    self.headBtn.layer.cornerRadius =self.headBtn.bounds.size.width/2.0;
    self.headBtn.layer.masksToBounds = YES;
    [self.headBtn addTarget:self action:@selector(headBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.headBtn];
    
    
    self.loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginBtn.frame = CGRectMake(NW(self.headBtn)+16.0f,42.0f, 120.0f, 30.0f);
    [self.loginBtn addTarget:self action:@selector(loginBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.loginBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.loginBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    [self.loginBtn setTitleColor:kColorTEXT forState:UIControlStateNormal];
    [self.bgView addSubview:self.loginBtn];
    
    
    self.loginLabel = [[UILabel alloc]initWithFrame:CGRectMake(X(self.loginBtn), NH(self.loginBtn), width(self.loginBtn), 30.0f)];
     self.loginLabel.textColor = [UIColor lightGrayColor];
     self.loginLabel.font = [UIFont systemFontOfSize:12.0f];
     self.loginLabel.text = @"登录后享受更多服务";
    [self.bgView addSubview: self.loginLabel];
    self.myTableView.tableHeaderView = self.bgView;
    
    UIView * footerView  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-32.0f, 80.0f)];

    //安全退出
    self.exitBtn = [[UIButton alloc]initWithFrame:CGRectMake(width(footerView)/2-(100.0f/2),16.0f,100.0f, 44.0f)];
    [self.exitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [self.exitBtn setBackgroundColor:kColorOrangeNormal forState:UIControlStateNormal];
    [self.exitBtn setBackgroundColor:kColorOrangePressed forState:UIControlStateHighlighted];
    [self.exitBtn setBackgroundColor:kColorOrangeDisabled forState:UIControlStateDisabled];
    self.exitBtn.layer.cornerRadius =  5.0;
    self.exitBtn.layer.masksToBounds = YES;
    [self.exitBtn setTitleColor:[UIColor hexStringToColor:@"#FFFFFF"] forState:UIControlStateNormal];
    [self.exitBtn addTarget:self action:@selector(exitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.exitBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14.0f];
    [footerView addSubview:self.exitBtn];
    

    
    self.myTableView.tableFooterView = footerView;
    self.firstTitleNameArr = [NSArray arrayWithObjects:@"个人资料",@"我的活动",@"重置密码",@"问题反馈",@"关于我们", nil];
    
    
}

#pragma mark -  tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        
        UIView * separtor = [[UIView alloc]initWithFrame:CGRectMake(0.0f, 59.5f, SCREEN_WIDTH - 32.0f, 0.5)];
        separtor.backgroundColor =  kColorGraySperator;
        separtor.tag = 111;
        [cell.contentView addSubview:separtor];//分割线
        
        
        
        UILabel *mainLabel = [[UILabel alloc]init];
        mainLabel.tag = 2;
        mainLabel.frame = CGRectMake(24.0f,16.0f ,100.0f,28.0f );
        mainLabel.font = [UIFont systemFontOfSize:16.0f];
        mainLabel.textColor = kColorTEXT;
        [cell.contentView addSubview:mainLabel];
        
    }
    UIView *separtor = (UIView*)[cell.contentView viewWithTag:111];
    if (indexPath.row==4) {
        separtor.hidden = YES;
    }else{
        separtor.hidden = NO;
    }
    
    
    
    UILabel *mainLabel = (UILabel*)[cell.contentView viewWithTag:2];
    mainLabel.text = [self.firstTitleNameArr objectAtIndex:indexPath.row];
    return cell;
    
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * backView = [[UIView alloc]init];
    backView.backgroundColor = [UIColor hexStringToColor:@"#f3f3f3"];
    return backView;
    
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * backview = [[UIView alloc]init];
    backview.backgroundColor = [UIColor hexStringToColor:@"#f3f3f3"];
    return backview;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row==0) {
        //我的活动
        
        if ([AccountTools isLogin]==NO) {
            
            LoginViewController *loginVC = [[LoginViewController alloc]init];
            loginVC.backBtHide = FALSE;
            [self presentViewController:loginVC animated:YES completion:^(void){}];
            
        }else{
        MyInfoViewController  *myInfoVC = [[MyInfoViewController alloc]init];
        [self.navigationController pushViewController:myInfoVC animated:YES];


            
       }
        
    }else if (indexPath.row==1){
        
        //个人资料
        if ([AccountTools isLogin]==NO) {
            
            LoginViewController *loginVC = [[LoginViewController alloc]init];
            loginVC.backBtHide = FALSE;
            [self presentViewController:loginVC animated:YES completion:^(void){}];
            
        }else{
            
            OfflineViewController *offlineVC = [[OfflineViewController alloc]init];
            [self.navigationController pushViewController:offlineVC animated:YES];
            
        }
   
        
       
    }else if (indexPath.row==2){
            //重置密码
            if ([AccountTools isLogin]==NO) {
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                loginVC.backBtHide = FALSE;
                [self presentViewController:loginVC animated:YES completion:^(void){}];
                
            }else{
                
                ChangePswViewController *changePswVC = [[ChangePswViewController alloc]init];
                [self.navigationController pushViewController:changePswVC animated:YES];
            }

            
        }else if (indexPath.row==3){
            //问题反馈
            
            ProblemFeedBackViewController *problemFeedBackVC = [[ProblemFeedBackViewController alloc]init];
            [self.navigationController pushViewController:problemFeedBackVC animated:YES];
        
        }else{
            //关于我们
            
            AboutUsViewController *aboutUsVC = [[AboutUsViewController alloc]init];
            [self.navigationController pushViewController:aboutUsVC animated:YES];
        }
    
}

#pragma mark -  actionSheet
- (void)headBtnAction
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"上传头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"拍照", nil];
    [actionSheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        
        //相册
        [self showTheImagePickerController:1];
        
        
    }else if(buttonIndex == 1){
        //拍照
        [self showTheImagePickerController:2];
        
    }else{
        //取消
    }
    
    
}

#pragma mark - 安全退出按钮
- (void)exitBtnClick:(UIButton*)btn
{
    
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"是否想要退出登录?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    
    
   

    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex==1) {
        
        [AccountTools exitLogin];
        self.exitBtn.hidden = YES;
        [self.loginBtn setTitle:@"注册/登录" forState:UIControlStateNormal];
        self.loginBtn.frame = CGRectMake(NW(self.headBtn)+10.0f,42.0f, 120.0f, 30.0f);
        [self.loginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.loginBtn.titleLabel.font = [UIFont systemFontOfSize:17.0f];
        self.loginBtn.enabled = YES;
        self.exitBtn.hidden = YES;
        self.loginLabel.hidden = NO;
        self.headBtn.enabled = NO;
        [self.headBtn setImage:[UIImage imageNamed:@"img_default_head"] forState:UIControlStateDisabled];

    }
    
}
#pragma mark -  拍照或相册选取
- (void)showTheImagePickerController:(int)carmerType{
    
    NSInteger sourceType;
    if (carmerType ==1) {
        sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    }else if(carmerType == 2){
        sourceType = UIImagePickerControllerSourceTypeCamera;
    }else{
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
    imgPicker.delegate = self;
    imgPicker.allowsEditing = YES;
    imgPicker.sourceType = sourceType;
    [imgPicker.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil]];
    [self presentViewController:imgPicker animated:YES completion:^{
        // 改变状态栏的颜色  为正常  这是这个独有的地方需要处理的
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }];
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
     [[AnimationIndicator sharedIndicator] startAnimationWithLoadText:@"加载中..."];
    
    
    [self dismissViewControllerAnimated:YES completion:^{
       
        //上传头像
        NSString * dataStr = [AppGlobalClass PostImagesToServer:UPDATEUSERHEADPHOTO dicPostParams:(NSMutableDictionary*)@{@"token":[AccountTools getUserToken]} dicImages:(NSMutableDictionary*)@{@"HeadPhoto":image}];
        NSDictionary *dictionary = [AppGlobalClass dictionaryWithJsonString:dataStr];
        
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
        {
            [[NSUserDefaults standardUserDefaults]setObject:[[[[[dictionary objectForKey:@"responseData"] firstObject]objectForKey:@"rows"] firstObject] objectForKey:@"userHeadPhoto"] forKey:@"userHeadPhoto"];
            ALERT_VIEW(@"上传成功");
            
            [self.headBtn setImage :image forState:UIControlStateNormal];
            
            [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
            
        }
        else{
            if ([[dictionary objectForKey:@"message"] isEqualToString:@"登录失败，请重新登录"])
            {
                
            }else
            {
                if ([CheckNetWork isExistenceNetwork]==FALSE) {
                    ALERT_VIEW(@"请检查网络");
                    
                }
                else{
                    ALERT_VIEW([dictionary objectForKey:@"message"]);
                    
                }
            }
            
            [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
        }

    }];
    
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)loginBtnAction:(id)sender
{
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    loginVC.backBtHide = FALSE;
    [self presentViewController:loginVC animated:TRUE completion:^(void){}];
}

@end
